home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 85 / CD Temático 40 Febrero 2004.iso / DOS / testdisk / src / common.c < prev    next >
Encoding:
C/C++ Source or Header  |  2004-01-10  |  2.9 KB  |  137 lines

  1. /*
  2.  
  3.     File: common.c
  4.  
  5.     Copyright (C) 1998-2004 Christophe GRENIER <grenier@cgsecurity.org>
  6.   
  7.     This software is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 2 of the License, or
  10.     (at your option) any later version.
  11.   
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.   
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program; if not, write to the Free Software
  19.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21.  */
  22.  
  23.  
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>    /* malloc */
  27. #include <stdarg.h>
  28. #include "types.h"
  29. #include "common.h"
  30. #include "lang.h"
  31. #include <ctype.h>      /* toupper, tolower */
  32. #include <string.h>    /* memset */
  33.  
  34. static unsigned int up2power_aux(const unsigned int number);
  35.  
  36. void *MALLOC(size_t size)
  37. {
  38.   void *res;
  39.   if(size==0)
  40.   {
  41.     ecrit_rapport("Try to allocate 0 byte of memory\n");
  42.     exit(EXIT_FAILURE);
  43.   }
  44.   if((res=malloc(size))==NULL)
  45.   {
  46.     ecrit_rapport(msg_MALLOC_ERROR);
  47.     exit(EXIT_FAILURE);
  48.   }
  49.   memset(res,0,size);
  50.   return res;
  51. }
  52.  
  53. void FREE(void *ptr)
  54. {
  55.   if(ptr==NULL)
  56.   {
  57.     wdoprintf(stdscr,msg_FREE_ERROR);
  58.     exit(EXIT_FAILURE);
  59.   }
  60.   free(ptr);
  61. }
  62.  
  63. #ifndef HAVE_SNPRINTF
  64. int snprintf(char *str, size_t size, const char *format, ...)
  65. {
  66.   int res;
  67.   va_list ap;
  68.   va_start(ap,format);
  69.   res=vsnprintf(str, size, format, ap);
  70.   va_end(ap);
  71.   return res;
  72. }
  73. #endif
  74.  
  75. #ifndef HAVE_VSNPRINTF
  76. int vsnprintf(char *str, size_t size, const char *format, va_list ap)
  77. {
  78.   return vsprintf(str,format,ap);
  79. }
  80. #endif
  81.  
  82. unsigned int up2power(const unsigned int number)
  83. {
  84. /*  ecrit_rapport("up2power(%u)=>%u\n",number, (1<<up2power_aux(number-1))); */
  85.   return (1<<up2power_aux(number-1));
  86. }
  87.  
  88. static unsigned int up2power_aux(const unsigned int number)
  89. {
  90.   if(number==0)
  91.     return 0;
  92.   else
  93.     return(1+up2power_aux(number/2));
  94. }
  95.  
  96. int check_volume_name(const char *name,const unsigned int max_size)
  97. {
  98.   unsigned int i;
  99.   for(i=0;name[i]!='\0' && i<max_size;i++)
  100.   {
  101.     if((name[i]>=0x6 && name[i]<=0x1f)||
  102.     (name[i]>=0x3A && name[i]<=0x3f))
  103.       return 1;
  104.     switch(name[i])
  105.     {
  106.       case 0x1:
  107.       case 0x2:
  108.       case 0x3:
  109.       case 0x4:
  110.       case 0x22:
  111.       case 0x2A:
  112.       case 0x2B:
  113.       case 0x2C:
  114. /*     case 0x2E: Pas sur */
  115.       case 0x2F:
  116.       case 0x5B:
  117.       case 0x5C:
  118.       case 0x5D:
  119.       case 0x7C:
  120. /*     case 'a' ... 'z': */
  121.     return 1;
  122.     }
  123.   }
  124.   return 0; /* Ok */
  125. }
  126.  
  127. void set_part_name(t_diskext *partition,const char *src,const int max_size)
  128. {
  129.   int i;
  130.   for(i=0;(i<max_size) && (src[i]!=(char)0);i++)
  131.     partition->name[i]=src[i];
  132.   partition->name[i--]='\0';
  133.   for(;(i>=0) && (src[i]==' ');i--);
  134.   partition->name[i+1]='\0';
  135. }
  136.  
  137.